﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace Z2.Editor
{
	public partial class BackgroundEditor : Form
	{
		
		private World m_map;
		private List<string> m_backgrounds;

		public BackgroundEditor(World map, List<string> backgrounds)
		{
			InitializeComponent();
			this.m_map = map;
			this.m_backgrounds = new List<string>(backgrounds.ToArray());
			this.m_backgrounds.Insert(0, string.Empty);
		}

		protected override void OnLoad(EventArgs e)
		{
			base.OnLoad(e);
			this.cbMainBackground.DataSource = this.m_backgrounds;
			this.cbMainBackground.SelectedIndex = this.m_backgrounds.IndexOf(this.m_map.Background);

			this.GeneratePreview();
		}

		private void btOK_Click(object sender, EventArgs e)
		{
			this.m_map.Background = Convert.ToString(this.cbMainBackground.SelectedItem);
			this.DialogResult = DialogResult.OK;
		}

		private void GeneratePreview()
		{
			Bitmap canvas = new Bitmap(pbPreview.Width, pbPreview.Height);
			string mainBackground = Convert.ToString(this.cbMainBackground.SelectedItem);
			using (Graphics graphics = Graphics.FromImage(canvas))
			{
				graphics.Clear(Color.White);
				if (this.cbMainBackground.SelectedIndex > -1 && !string.IsNullOrEmpty(mainBackground))
				{
					Bitmap bmp = (Bitmap)Bitmap.FromFile(string.Format(@"Objects\{0}", this.cbMainBackground.SelectedItem));
					graphics.DrawImage(bmp, 0, 0, pbPreview.Width, pbPreview.Height);
				}
			}

			this.pbPreview.Image = canvas;
		}

		private void cbMainBackground_SelectedIndexChanged(object sender, EventArgs e)
		{
			this.GeneratePreview();
		}
	}
}
